<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Services_Hook
{
	public static function add($hook)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Core_Models_Mysql_Hook();
		$dao->setConnection($conn);
		return $dao->add($hook);
	}
	
	public static function delete($hookId)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Core_Models_Mysql_Hook();
		$dao->setConnection($conn);
		return $dao->delete($hookId);
	}
	
	public static function dbHooks($module, $all = false)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Core_Models_Mysql_Hook();
		$dao->setConnection($conn);
		return $dao->dbHooks($module, $all);
	}
	
	public static function exist($hook)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Core_Models_Mysql_Hook();
		$dao->setConnection($conn);
		return $dao->exist($hook);
	}
	
	public static function getHooks($module = null)
	{
//		$conn		= Gio_Db_Connection::getConnection();
//		$dao = new Modules_Core_Models_Mysql_Hook();
//		$dao->setConnection($conn);
//		return $dao->getHooks($module);
		
		$ret = array();
		$hooksDir = ROOT_DIR . DS . 'hooks';
		
		if ($module) {
			$hooksDir = MOD_DIR . DS . $module . DS . 'hooks';
		}
		
		if (!file_exists($hooksDir)) {
			return $ret;
		}
		$hooksIterator = new DirectoryIterator($hooksDir);
		foreach ($hooksIterator as $hook) {
			if ($hook->isDot()) {
				continue;
			}
			$hookName = $hook->getFilename();
			if (('CVS' == $hookName) || ('.svn' == strtolower($hookName)) 
					|| ('index.html' == $hookName)
					|| ('.htaccess' == $hookName)) {
                continue;
            }
            /**
             * Widget information
             */
            $xmlFile = $hooksDir . DS . $hookName . DS . 'about.xml';
            if (file_exists($xmlFile)) {
	            $xmlData = @simplexml_load_file($xmlFile);
	            $data = array(
	            	'name'			=> strtolower((string)$xmlData->name),
	            	'module'		=> strtolower((string)$xmlData->module),
	            	'thumbnail'		=> (string)$xmlData->thumbnail,
	            	'author'		=> (string)$xmlData->author,
	            	'email'  		=> (string)$xmlData->email,
	            	'description'	=> (string)$xmlData->description,
	            	'version'		=> (string)$xmlData->version,
	            	'license'		=> (string)$xmlData->license,
	            	'params'		=> Gio_Core_Hook_Config::getParams($hookName, $module)
	            );
	            $ret[] = $data;
            }
		}
		return $ret;
	}
	
	public static function getModules()
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Core_Models_Mysql_Hook();
		$dao->setConnection($conn);
		return $dao->getModules();
	}
}